import sys
import csv_util
import xml_util

def usage():
    print("convert csv to xml: ", end='')
    print("driver.py -c [input_csv_file] -x [output_xml_file]")
    print("convert xml to csv: ", end='')
    print ("driver.py -x [input_xml_file] -c [output_csv_file]")
    

def check_argv(argv):
    if (len(sys.argv) is not 5):
        return 0
    if  (argv[1]  != "-c" and argv[3] != "-c"):
        return 0
    if  (argv[1]  != "-x" and argv[3] != "-x"):
        return 0
    if (argv[1] == '-c' ): 
        return 1
    else:
        return 2


def main():
    
    run_type = check_argv(sys.argv)
    
    if( run_type == 0):
        usage()
        sys.exit()
    
    #the place holder for all objects
    schools = {}
    
    if(run_type ==1):
        infile = sys.argv[2]
        outfile = sys.argv[4]
        csv_util.load_csv(schools, infile)
        xml_util.write2xml(schools, outfile)
        print('converted {} to {}'.format(sys.argv[2], sys.argv[4]));
    
    
    else: 
        infile = sys.argv[2]
        outfile = sys.argv[4]
        xml_util.loadxml(schools, infile)
        csv_util.writecsv(schools, outfile)
        print('converted {} to {}'.format(sys.argv[2], sys.argv[4]));
        

if __name__ == "__main__": main()  